package org.minuframework.mybatis;

import java.util.Map;
import org.minuframework.json.GsonUtils;
import org.minuframework.utils.LangUtils;
import com.google.common.collect.Maps;

/**
 * @author 米mi
 *
 */
public abstract class JsonResult {

    /**
     * 消息码
     */
    private int code;
    /**
     * 消息
     */
    private String msg;
    /**
     * 数据
     */
    private Object data;
    /**
     * 总记录数
     */
    private long total;
    /**
     * 总页数
     */
    private long pages;
    /**
     * 当前页
     */
    private int pageNum;
    /**
     * 每页的数量
     */
    private int pageSize;
    /**
     * 是否成功
     */
    private boolean success;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public long getPages() {
        return pages;
    }

    public void setPages(long pages) {
        this.pages = pages;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public boolean success() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Map<String, Object> getReturnMap() {
        Map<String, Object> returnMap = Maps.newHashMap();
        returnMap.put("code", this.code);
        returnMap.put("msg", this.msg);
        returnMap.put("data", this.data);
        returnMap.put("total", this.total);
        returnMap.put("pages", this.pages);
        returnMap.put("pageNum", this.pageNum);
        returnMap.put("pageSize", this.pageSize);
        returnMap.put("success", this.success);
        return returnMap;
    }

    public void setReturnMap(Map<String, Object> returnMap) {
        if (returnMap != null && !returnMap.isEmpty()) {
            this.code = returnMap.containsKey("code")
                    ? LangUtils.parseInt(returnMap.get("code").toString())
                    : this.code;
            this.msg = returnMap.containsKey("msg") ? returnMap.get("msg").toString() : this.msg;
            this.data = returnMap.containsKey("data") ? returnMap.get("data") : this.data;
            this.total = returnMap.containsKey("total")
                    ? LangUtils.parseInt(returnMap.get("total").toString())
                    : this.total;
            this.pages = returnMap.containsKey("pages")
                    ? LangUtils.parseInt(returnMap.get("pages").toString())
                    : this.pages;
            this.pageNum = returnMap.containsKey("pageNum")
                    ? LangUtils.parseInt(returnMap.get("pageNum").toString())
                    : this.pageNum;
            this.pageSize = returnMap.containsKey("pageSize")
                    ? LangUtils.parseInt(returnMap.get("pageSize").toString())
                    : this.pageSize;
            this.success = returnMap.containsKey("success")
                    ? LangUtils.parseBoolean(returnMap.get("success").toString())
                    : this.success;
        }
    }

    public String toJson() {
        return GsonUtils.toGSONString(this);
    }

    public abstract String toEJson();

}
